package com.datang.pet.api.talkController;/*
 * @auther 顶风少年
 * @mail dfsn19970313@foxmail.com
 * @date 2019-06-13 08:51
 * @notify
 * @version 1.0
 */

import com.datang.pet.common.redis.RedisTool;
import com.datang.pet.common.response.Result;
import com.datang.pet.common.utils.MD5;
import com.datang.pet.common.utils.StringUtils;
import com.datang.pet.service.TalkService;
import com.datang.pet.vo.GetTalkListResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;

@RestController
public class GetTalkListController {

    @Autowired
    private TalkService talkService;

    @Autowired
    private RedisTool redisTool;


    /*
     * @author 顶风少年
     * @date 2019/7/1 16:18
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @param time
     * @result com.datang.pet.common.response.Result
     * @notify 获取日志列表
     * version 1.0
     */
    @GetMapping("/getTalkList")
    public Result getTalkList(Date time) throws Exception {
        return Result.ok(talkService.getTalkList(time));
    }


    /*
     * @author 顶风少年
     * @date 2019/7/1 16:18
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @param userId
     * @result com.datang.pet.common.response.Result
     * @notify 获取用户发布的日志
     * version 1.0
     */
    @GetMapping("/getTalkListByUserId")
    public Result getTalkListByUserId(String userId) throws Exception {
        //从redis中取用户喵文
        ArrayList talkList = (ArrayList<GetTalkListResponse>) redisTool.get("talkListByUserId" + userId);
        if (talkList == null) {
            //取不到则到mysql中取
            talkList = (ArrayList) talkService.getTalkListByUserId(userId);

            redisTool.set("talkListByUserId" + userId, talkList);
        }
        return Result.ok(talkList);
    }


    /*
     * @author 顶风少年
     * @date 2019/7/1 16:18
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @param userId
     * @param keyword
     * @param time
     * @result com.datang.pet.common.response.Result
     * @notify 搜索喵文
     * version 1.0
     */
    @GetMapping("/getTalkListByKeyword")
    public Result getTalkListByKeyword(String userId, String keyword, Date time) throws Exception {
        try {
            userId = (MD5.decryptData(userId));
        } catch (Exception e) {
            throw new Exception("userId解密出错！");
        }
        if (!StringUtils.isNUll(userId)) {
            throw new Exception("用户id不能为空");
        }
        if (!StringUtils.isNUll(keyword)) {
            throw new Exception("关键字不能为空");
        }
        return Result.ok(talkService.getTalkListByKeyword(userId, keyword, time));
    }
}
